﻿/*************************************************************************
 *  Copyright (C), 2015-2016, Mogoson Tech. Co., Ltd.
 *------------------------------------------------------------------------
 *  File         :  TextureExtention.cs
 *  Description  :  Extention of UnityEngine.Texture2D.
 *------------------------------------------------------------------------
 *  Author       :  Mogoson
 *  Version      :  0.1.0
 *  Date         :  10/24/2015
 *  Description  :  Initial development version.
 *************************************************************************/

using Developer.Converter;
using UnityEngine;

namespace Developer.TextureExtention
{
    /// <summary>
    /// Extend Texture2D's functions.
    /// </summary>
    public static class ETexture2D
    {
        #region Public Static Method
        /// <summary>
        /// Update Texture2D's Pixels.
        /// </summary>
        public static void UpdatePixels(this Texture2D texture2D, Color[] colorArray, int mipLevel = 0, bool updateMipmaps = false, bool makeNointerReadable = false)
        {
            if (colorArray == null || colorArray.Length != texture2D.width * texture2D.height)
                return;
            texture2D.SetPixels(colorArray, mipLevel);
            texture2D.Apply(updateMipmaps, makeNointerReadable);
        }

        /// <summary>
        /// Update Texture2D's Pixels.
        /// </summary>
        public static void UpdatePixels(this Texture2D texture2D, Color[,] colorArray, int mipLevel = 0, bool updateMipmaps = false, bool makeNointerReadable = false)
        {
            if (colorArray == null || colorArray.Length != texture2D.width * texture2D.height)
                return;
            Color[] cArray = ArrayConverter.ToOneDimentionArray(colorArray);
            UpdatePixels(texture2D, cArray, mipLevel, updateMipmaps, makeNointerReadable);
        }

        /// <summary>
        /// Update Texture2D's Pixels.
        /// </summary>
        public static void UpdatePixels(this Texture2D texture2D, Color32[] colorArray, int mipLevel = 0, bool updateMipmaps = false, bool makeNointerReadable = false)
        {
            if (colorArray == null || colorArray.Length != texture2D.width * texture2D.height)
                return;
            texture2D.SetPixels32(colorArray, mipLevel);
            texture2D.Apply(updateMipmaps, makeNointerReadable);
        }

        /// <summary>
        /// Update Texture2D's Pixels.
        /// </summary>
        public static void UpdatePixels(this Texture2D texture2D, Color32[,] colorArray, int mipLevel = 0, bool updateMipmaps = false, bool makeNointerReadable = false)
        {
            if (colorArray == null || colorArray.Length != texture2D.width * texture2D.height)
                return;
            Color32[] cArray = ArrayConverter.ToOneDimentionArray(colorArray);
            UpdatePixels(texture2D, cArray, mipLevel, updateMipmaps, makeNointerReadable);
        }
        #endregion
    }
}